package com.yydm.serviceedu.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yydm.serviceedu.entity.AutomobileCategory;
import com.yydm.serviceedu.entity.CategoryData;
import com.yydm.serviceedu.entity.category.OneCategory;
import com.yydm.serviceedu.entity.category.TwoCategory;
import com.yydm.serviceedu.listener.CategoryExcelListener;
import com.yydm.serviceedu.mapper.AutomobileCategoryMapper;
import com.yydm.serviceedu.service.AutomobileCategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author yydm
 * @since 2022-06-25
 */
@Service
public class AutomobileCategoryServiceImpl extends ServiceImpl<AutomobileCategoryMapper, AutomobileCategory> implements AutomobileCategoryService {

    @Autowired
    private AutomobileCategoryService automobileCategoryService;

    @Override
    public void saveCategory(MultipartFile file, AutomobileCategoryService automobileCategoryService) {

        try {
//            文件输入流
            InputStream inputStream = file.getInputStream();
//            调用方法进行调试
            EasyExcel.read(inputStream, CategoryData.class, new CategoryExcelListener(automobileCategoryService)).sheet().doRead();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    //    课程分类列表（树形）
    @Override
    public List<OneCategory> getAllOneTwoCategory() {
//        查询一级分类 parent_id == 0
        QueryWrapper<AutomobileCategory> wrapperOne = new QueryWrapper();
        wrapperOne.eq("parent_id", 0);
        List<AutomobileCategory> OneCategory = automobileCategoryService.list(wrapperOne);

//        查询二级分类 parent_id <>= 0
        QueryWrapper<AutomobileCategory> wrapperTwo = new QueryWrapper<>();
        wrapperTwo.ne("parent_id", 0);
        List<AutomobileCategory> TwoCategory = automobileCategoryService.list(wrapperTwo);

//        创建list集合，用于存储最终封装数据
        List<OneCategory> finalCategoryList = new ArrayList<>();

//        封装一级分类
//        查询所有一级分类list集合遍历,得到每个一级分类对象,获取每个一级分类对象值
//        封装到要求的list集合里面List<OneCategory>
        for (int i = 0; i < OneCategory.size(); i++) {  //遍历OneCategory集合
//            得到OneCategory每个AutomobileCategory对象
            AutomobileCategory automobileCategory = OneCategory.get(i);

//            把automobileCategory里面值获取出来,放到OneCategory对象里面
            OneCategory oneCategory = new OneCategory();
//           oneCategory.setId(automobileCategory.getId());
//           oneCategory.setName(automobileCategory.getName());
//            属性自动赋值 将automobileCategory值赋值到oneCategory里面去
            BeanUtils.copyProperties(automobileCategory, oneCategory);
            finalCategoryList.add(oneCategory);

//            在一级分类循环遍历查询所有的二级分类
//            创建list集合封装每一个一级分类的二级分类
            List<TwoCategory> twoFinalCategoryList = new ArrayList<>();
//            遍历二级分类list集合
            for (int m = 0; m < TwoCategory.size(); m++) {
//                获取每个二级分类
                AutomobileCategory twoAutomobileCategory = TwoCategory.get(m);
//               判断二级分类parentid和一级分类id是否一样
                if (twoAutomobileCategory.getParentId() == automobileCategory.getId()) {
//                    把twoAutomobileCategory值复制到TwoCategory,放到twoFinalCategoryList
                    TwoCategory twoCategory = new TwoCategory();
                    BeanUtils.copyProperties(twoAutomobileCategory, twoCategory);
                    twoFinalCategoryList.add(twoCategory);
                }
            }
//            把一级下面所有二级分类放到一级分类里面
            oneCategory.setChildren(twoFinalCategoryList);
        }

        return finalCategoryList;
    }

}
